//
//  ActivateViewController.m
//  IBaby
//
//  Created by ZengYuan on 14/11/18.
//  Copyright (c) 2014年 com.zhongwei. All rights reserved.
//

#import "SetpasswdViewController.h"
//#import "LoginedController.h"
#import "UserUnit.h"
#import "AppDelegate.h"
#import "HomeViewController.h"
#import "UserUnit.h"
#import "UserReg.h"

//#import "UserLogin.h"
//#import "SupplementViewController.h"

@interface SetpasswdViewController ()

@end

@implementation SetpasswdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
//    HorizonLine *line = [[HorizonLine alloc]initWithFrame:CGRectMake(0, 0, inputCoverView.frame.size.width, 1)];
//    [inputCoverView addSubview:line];
//    [line setLineColor:RGB(150, 141, 139)];
//    
//    line = [[HorizonLine alloc]initWithFrame:CGRectMake(44, inputCoverView.frame.size.height/2, inputCoverView.frame.size.width, 1)];
//    [inputCoverView addSubview:line];
//    [line setLineColor:RGB(150, 141, 139)];
//    
//    
//    line = [[HorizonLine alloc]initWithFrame:CGRectMake(0, inputCoverView.frame.size.height-1, inputCoverView.frame.size.width, 1)];
//    [inputCoverView addSubview:line];
//    [line setLineColor:RGB(150, 141, 139)];
    
//    UIEdgeInsets insets = UIEdgeInsetsMake(0, 50, 0, 50);
//    UIImage *streImage = [[UIImage imageNamed:@"redbtn_bk"] resizableImageWithCapInsets:insets];
//    [activateBtn setBackgroundImage:streImage forState:UIControlStateNormal];
    coverView.layer.cornerRadius=5;
    [self setBtnGreenBackgroundImage:activateBtn];
//    [self addTwoLinesFor:coverView];
    
    [seePwdBtn setImage:[UIImage imageNamed:@"icon_showpasswd"] forState:UIControlStateNormal];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.titleLabel.text=@"设置密码";
//    self.navigationItem.title=@"设置密码";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)activate:(id)sender{
    
    if ([passwdField.text length] == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"密码不能为空" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
        return;
    }
    
    NSString* rule1 = @"^[0-9a-zA-Z_]{6,20}$";
    NSPredicate* pred=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",rule1];
    BOOL isMatch=[pred evaluateWithObject:passwdField.text];
    if(!isMatch){

        MBALERT(@"密码格式不对");
        return;
    }
 
    if ([passwdField.text length]< 6 || [passwdField.text length]>20) {
        MBALERT(@"密码长度必须6-20位");
        return;
    }
 
    activateBtn.userInteractionEnabled=NO;

    UIWindow *window=[[UIApplication sharedApplication].windows lastObject];
    hud=[MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText=@"注册中";

    
    //找回密码
    if ( [[[NSUserDefaults standardUserDefaults] objectForKey:@"setpasswd_target"] isEqualToString:@"0"]) {
        
        [UserReg userSet:self.phone newpasswd:passwdField.text success:^{
            hud.mode=MBProgressHUDModeText;
            hud.labelText=@"设置成功";
            [hud hide:YES afterDelay:0.5];
            [self.navigationController popToRootViewControllerAnimated:YES];
        } failure:^(NSString *error) {
            hud.mode=MBProgressHUDModeText;
            hud.labelText=HTTPERROR_DATAFAIL;
            [hud hide:YES afterDelay:0.5];
            activateBtn.userInteractionEnabled=YES;
        }];
        
    }else{
        //注册
        
        [UserReg userReg:self.phone andPwd:passwdField.text success:^{
            
            hud.mode=MBProgressHUDModeText;
            hud.labelText=@"注册成功";
            [hud hide:YES afterDelay:0.5];
            
            [self.navigationController popToRootViewControllerAnimated:YES];
        } failure:^(NSString *error) {
            hud.mode=MBProgressHUDModeText;
            hud.labelText=HTTPERROR_DATAFAIL;
            [hud hide:YES afterDelay:0.5];
            activateBtn.userInteractionEnabled=YES;
            
        }];

        
    }
    
    
 
    
}



-(void)goback{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

-(IBAction)seePasswd:(id)sender{

    if (passwdField.secureTextEntry) {
        passwdField.secureTextEntry=NO;
        [seePwdBtn setImage:[UIImage imageNamed:@"icon_showpasswd_n"] forState:UIControlStateNormal];
    }else{
        passwdField.secureTextEntry=YES;
        [seePwdBtn setImage:[UIImage imageNamed:@"icon_showpasswd"] forState:UIControlStateNormal];
    }
}


/**
 *  激活成功，直接登录
 */


-(void)navLeftAction{
    [self.navigationController trvs_popViewControllerWithNavigationControllerTransition];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
